package com.huawei.m2m.cig.decoder.hECP.wECP32.generate.adaptor;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.huawei.m2m.cig.decoder.hECP.wECP32.generate.factory.DefaultCloudMessageFactory;
import com.huawei.m2m.cig.decoder.hECP.wECP32.generate.factory.DefaultMessageFactory;
import com.huawei.m2m.cig.decoder.protocol.BigEndianInputBuffer;
import com.huawei.m2m.cig.decoder.protocol.BigEndianOutputBuffer;
import com.huawei.m2m.cig.decoder.protocol.InputBuffer;
import com.huawei.m2m.cig.decoder.protocol.NBMessage;
import com.huawei.m2m.cig.decoder.protocol.OutputBuffer;
import com.huawei.m2m.cig.tup.modules.protocol_adapter.IProtocolAdapter;
import java.util.Arrays;
import org.osgi.framework.BundleContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ProtocolAdapterImpl implements IProtocolAdapter {
	private static final Logger LOGGER = LoggerFactory.getLogger(ProtocolAdapterImpl.class);
	private static final String MANU_FACTURERID = "ECP";
	private static final String MODEL = "ECP32";

	public void activate(BundleContext paramBundleContext) {
		LOGGER.info("ProtocolAdapterImpl activate");
	}

	public void deactivate(BundleContext paramBundleContext) {
		LOGGER.info("ProtocolAdapterImpl deactivate");
	}

	public ObjectNode decode(byte[] paramArrayOfByte) throws Exception {
		if (paramArrayOfByte == null) {
			throw new NullPointerException("rawData is null");
		}
		LOGGER.info("receive raw message {}", Arrays.toString(paramArrayOfByte));

		int i = getMessageId(paramArrayOfByte);
		NBMessage localNBMessage = DefaultMessageFactory.getInstance().createMessage(i);
		if (localNBMessage == null) {
			LOGGER.error("msg  id={} not found!", Integer.valueOf(i));
			return null;
		}
		BigEndianInputBuffer localBigEndianInputBuffer = new BigEndianInputBuffer(paramArrayOfByte);
		localNBMessage.decode(localBigEndianInputBuffer);
		LOGGER.info("decode jsonNode is {}", localNBMessage.toJsonNode());
		return localNBMessage.toJsonNode();
	}

	private int getMessageId(byte[] paramArrayOfByte) {
		byte[] arrayOfByte = new byte[1];
		System.arraycopy(paramArrayOfByte, 0, arrayOfByte, 0, 1);
		BigEndianInputBuffer localBigEndianInputBuffer = new BigEndianInputBuffer(arrayOfByte);
		return localBigEndianInputBuffer.getByte();
	}

	public byte[] encode(ObjectNode paramObjectNode) throws Exception {
		if (paramObjectNode == null) {
			LOGGER.error("objectNode == null");
			return null;
		}
		LOGGER.info("encode jsonNode is {}", paramObjectNode);
		if (!paramObjectNode.has("msgType")) {
			LOGGER.error("msgType not found");
			return null;
		}
		String str = paramObjectNode.get("msgType").asText();
		if ("cloudReq".equals(str)) {
			return encodeCloudRequest(paramObjectNode);
		}
		if ("cloudRsp".equals(str)) {
			return encodeCloudResponse(paramObjectNode);
		}
		LOGGER.error(String.format("invalid msgType {}", new Object[] { str }));
		return null;
	}

	private byte[] encodeCloudRequest(ObjectNode paramObjectNode) throws Exception {
		if (!paramObjectNode.has("cmd")) {
			LOGGER.error("cmd not found, objectNode is {}", paramObjectNode);
			return null;
		}
		String str = paramObjectNode.get("cmd").asText();

		return encodeHelper(paramObjectNode, str);
	}

	private byte[] encodeCloudResponse(ObjectNode paramObjectNode) throws Exception {
		String str = DefaultCloudMessageFactory.getInstance().getCloudMessageRspName();
		if (isStringEmpty(str)) {
			return null;
		}
		byte[] arrayOfByte = new byte[4];
		arrayOfByte[0] = -86;
		arrayOfByte[1] = -86;
		arrayOfByte[2] = 0;
		arrayOfByte[3] = 0;
		return arrayOfByte;
	}

	private byte[] encodeHelper(ObjectNode paramObjectNode, String paramString) throws Exception {
		if (isStringEmpty(paramString)) {
			return null;
		}
		NBMessage localNBMessage = DefaultCloudMessageFactory.getInstance().createMessage(paramString);
		if (localNBMessage == null) {
			return null;
		}
		localNBMessage.configJsonNode(paramObjectNode);

		BigEndianOutputBuffer localBigEndianOutputBuffer = new BigEndianOutputBuffer();
		localNBMessage.encode(localBigEndianOutputBuffer);

		LOGGER.info("result byte is {}", Arrays.toString(localBigEndianOutputBuffer.toByteArray()));

		return localBigEndianOutputBuffer.toByteArray();
	}

	public String getManufacturerId() {
		return "ECP";
	}

	public String getModel() {
		return "ECP32";
	}

	private boolean isStringEmpty(String paramString) {
		if ((paramString == null) || (paramString.isEmpty())) {
			return true;
		}
		return false;
	}
}
